import socket

# 创建TCP套接字
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# 绑定IP和端口号
server_socket.bind(("127.0.0.1", 8080))

# 监听客户端连接请求
server_socket.listen(5)

print("Server started and listening...")

while True:
    # 等待客户端连接请求
    client_socket, client_address = server_socket.accept()

    print(f"Client {client_address} connected.")

    # 接收客户端数据并回复
    data = client_socket.recv(1024)

    if data:
        print(f"Received data: {data.decode()}")
        client_socket.sendall(b"Thank you for connecting!")

    # 关闭客户端连接套接字
    client_socket.close()
